//
//  WithDrawView.m
//  lhpc_manager
//
//  Created by 李英杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "WithDrawView.h"

@interface WithDrawView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView * backView;

@property (nonatomic, strong) UILabel * chooseTypeLabel;
@property (nonatomic, strong) UIView * wxView;
@property (nonatomic, strong) UIButton * wxSelectButton;
@property (nonatomic, strong) UIButton * wxIconButton;


@property (nonatomic, strong) UIView * aliView;
@property (nonatomic, strong) UIButton * aliSelectButton;
@property (nonatomic, strong) UIButton * aliIconButton;
@property (nonatomic, strong) UITextField * aliTextField;

@property (nonatomic, strong) UIView * moneyView;
@property (nonatomic, strong) UILabel * moneyIconLabel;
@property (nonatomic, strong) UILabel * moneyLabel;


@property (nonatomic, strong) UIButton * withDrawButton;


@property (nonatomic, copy) NSString * withDrawAccount;

@end

@implementation WithDrawView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.isWXOrAli = 1;
        [self setMainView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}


-(void)comfirmWithDraw{
    
    if ([self.delegate respondsToSelector:@selector(withDrawButtonHasClick:)]) {
        [self.delegate withDrawButtonHasClick:self.withDrawAccount];
    }
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}

-(void)setDefaultStyle:(LHPayType)type AndAccount:(NSString *)accountStr{
    
    if (type == LHPayTypeAliPay) {
        
        [self aliSelect];
        self.aliTextField.text = accountStr;
        self.wxTextField.placeholder = @"";
        
    }else if (type == LHPayTypeWXPay){
        
        [self wxSelect];
        self.wxTextField.text = accountStr;
        self.aliTextField.placeholder = @"";
    }
    
    [_withDrawButton setBackgroundColor:ORANGCOLOR];
    _withDrawButton.userInteractionEnabled = YES;
    
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    if (textField.text.length > 0) {
        [_withDrawButton setBackgroundColor:ORANGCOLOR];
        _withDrawButton.userInteractionEnabled = YES;
    }else{
        [_withDrawButton setBackgroundColor:GRAYCOLOR];
        _withDrawButton.userInteractionEnabled = NO;
    }
    
    self.withDrawAccount = textField.text;
    
    textField.placeholder = nil;
    
    return YES;
}

-(void)wxSelect{
    
    if (self.wxSelectButton.selected) {
        return;
    }
    self.isWXOrAli = 1;
    
    self.wxTextField.userInteractionEnabled = YES;
    self.wxSelectButton.selected = YES;
    self.wxIconButton.selected = YES;
    self.aliTextField.userInteractionEnabled = NO;
    self.aliSelectButton.selected = NO;
    self.aliIconButton.selected = NO;
    
    self.aliTextField.text = @"";
    [self.wxTextField becomeFirstResponder];
}

-(void)aliSelect{
    
    if (self.aliSelectButton.selected) {
        return;
    }
    self.isWXOrAli = 0;
    
    self.aliTextField.userInteractionEnabled = YES;
    self.aliSelectButton.selected = YES;
    self.aliIconButton.selected = YES;
    self.wxTextField.userInteractionEnabled = NO;
    self.wxSelectButton.selected = NO;
    self.wxIconButton.selected = NO;
    
    self.wxTextField.text = @"";
    [self.aliTextField becomeFirstResponder];
    
    
}

-(void)setCount:(CGFloat)count{
    
    self.moneyLabel.text = [NSString stringWithFormat:@"%.2f",count];
}

- (NSString *)getAccountText {
    if (self.isWXOrAli == 1) {
        return self.wxTextField.text;
    }else{
        return self.aliTextField.text;
    }
}

-(void)setMainView{
    
    [self addSubview:self.backView];
    [self.backView addSubview:self.chooseTypeLabel];
    
    [self.backView addSubview:self.wxView];
    [self.wxView addSubview:self.wxSelectButton];
    [self.wxView addSubview:self.wxIconButton];
    [self.wxView addSubview:self.wxTextField];
    
    [self.backView addSubview:self.aliView];
    [self.aliView addSubview:self.aliSelectButton];
    [self.aliView addSubview:self.aliIconButton];
    [self.aliView addSubview:self.aliTextField];
    
    
    [self.backView addSubview:self.moneyView];
    [self.moneyView addSubview:self.moneyIconLabel];
    [self.moneyView addSubview:self.moneyLabel];
    
    [self.backView addSubview:self.withDrawButton];
    
}

-(void)updateConstraints{
    [super updateConstraints];
    
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self);
        
    }];
    
    [self.chooseTypeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.right.equalTo(self.backView);
        make.left.equalTo(self.backView).offset(15);
        make.height.equalTo(@40);
        
    }];
    
    [self.wxView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.chooseTypeLabel.mas_bottom).offset(0);
        make.left.right.equalTo(self.backView);
        make.height.equalTo(@50);
        
    }];
    
    [self.wxSelectButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.wxView);
        make.right.equalTo(self.wxView).offset(0);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        
    }];
    
    
    [self.wxIconButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.wxView);
        make.right.equalTo(self.wxSelectButton.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(90, 50));
        
    }];
    
    
    [self.wxTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.bottom.equalTo(self.wxView);
        make.right.equalTo(self.wxIconButton.mas_left).offset(-5);
        make.left.equalTo(self.wxView).offset(15);
        
    }];
    
    [self.aliView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.wxView.mas_bottom).offset(5);
        make.left.right.equalTo(self.backView);
        make.height.equalTo(self.wxView);
        
    }];
    
    [self.aliSelectButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.aliView);
        make.right.equalTo(self.wxSelectButton);
        make.size.equalTo(self.wxSelectButton);
        
    }];
    
    
    [self.aliIconButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.aliSelectButton);
        make.right.equalTo(self.wxIconButton);
        make.size.equalTo(self.wxIconButton);
        
    }];
    
    
    [self.aliTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self.aliView);
        make.right.equalTo(self.wxTextField);
        make.left.equalTo(self.wxTextField);
        
    }];
    
    
    [self.moneyView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.aliView.mas_bottom).offset(15);
        make.left.right.equalTo(self.backView);
        make.height.equalTo(@100);
        
    }];
    
    [self.moneyIconLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.moneyView);
        make.left.equalTo(self.moneyView).offset(15);
        make.width.equalTo(@50);
        
    }];
    
    [self.moneyLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.moneyView);
        make.left.equalTo(self.moneyIconLabel.mas_right);
        
    }];
    
    
    [self.withDrawButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.moneyView.mas_bottom).offset(100);
        make.left.equalTo(self.backView).offset(30);
        make.right.equalTo(self.backView).offset(-30);
        make.height.equalTo(@40);
    }];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == self.wxTextField) {
        textField.placeholder = @"请输入您的微信账号";
    }else{
        textField.placeholder = @"请输入您的支付宝账号";
    }
    
    if (textField.text.length == 0) {
        [_withDrawButton setBackgroundColor:GRAYCOLOR];
        _withDrawButton.userInteractionEnabled = NO;
    }
    
    return YES;
}

#pragma mark - getter

-(UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = BACKCOLOR;
    }
    return _backView;
}

-(UILabel *)chooseTypeLabel{
    if (!_chooseTypeLabel) {
        _chooseTypeLabel = [[UILabel alloc]init];
        _chooseTypeLabel.textColor = BLACKCOLOR;
        _chooseTypeLabel.backgroundColor = BACKCOLOR;
        _chooseTypeLabel.font = [UIFont systemFontOfSize:16];
        _chooseTypeLabel.text = @"请选择账户类型";
    }
    return _chooseTypeLabel;
}

-(UIView *)wxView{
    if (!_wxView) {
        _wxView = [[UIView alloc]init];
        _wxView.backgroundColor = [UIColor whiteColor];
    }
    return _wxView;
}

-(UIButton *)wxSelectButton{
    if (!_wxSelectButton) {
        _wxSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_wxSelectButton setImage:[UIImage imageNamed:@"pay_normal"] forState:UIControlStateNormal];
        [_wxSelectButton setImage:[UIImage imageNamed:@"pay_select"] forState:UIControlStateSelected];
        _wxSelectButton.selected = YES;
        [_wxSelectButton addTarget:self action:@selector(wxSelect) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _wxSelectButton;
}

-(UIButton *)wxIconButton{
    if (!_wxIconButton) {
        _wxIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_wxIconButton setImage:[UIImage imageNamed:@"pay_icon_wx"] forState:UIControlStateNormal];
        [_wxIconButton setImage:[UIImage imageNamed:@"pay_icon_wx"] forState:UIControlStateSelected];
        [_wxIconButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
        [_wxIconButton setTitle:@"微信" forState:UIControlStateNormal];
        [_wxIconButton setTitleColor:BLACKCOLOR forState:UIControlStateSelected];
        [_wxIconButton setTitleColor:GRAYCOLOR forState:UIControlStateNormal];
        _wxIconButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _wxIconButton.adjustsImageWhenHighlighted = NO;
        _wxIconButton.selected = YES;
        [_wxIconButton addTarget:self action:@selector(wxSelect) forControlEvents:UIControlEventTouchUpInside];
//        [_wxIconButton setBackgroundColor:[UIColor lightGrayColor]];
        
    }
    return _wxIconButton;
}

-(UITextField *)wxTextField{
    if (!_wxTextField) {
        _wxTextField = [[UITextField alloc]init];
        _wxTextField.delegate = self;
        _wxTextField.returnKeyType = UIReturnKeyDone;
        _wxTextField.keyboardType = UIKeyboardTypeDefault;
        _wxTextField.placeholder = @"请输入您的微信账号";
    }
    return _wxTextField;
}

-(UIView *)aliView{
    if (!_aliView) {
        _aliView = [[UIView alloc]init];
        _aliView.backgroundColor = [UIColor whiteColor];
    }
    return _aliView;
}

-(UIButton *)aliSelectButton{
    if (!_aliSelectButton) {
        _aliSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_aliSelectButton setImage:[UIImage imageNamed:@"pay_normal"] forState:UIControlStateNormal];
        [_aliSelectButton setImage:[UIImage imageNamed:@"pay_select"] forState:UIControlStateSelected];
        [_aliSelectButton addTarget:self action:@selector(aliSelect) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _aliSelectButton;
}

-(UIButton *)aliIconButton{
    if (!_aliIconButton) {
        _aliIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_aliIconButton setImage:[UIImage imageNamed:@"pay_icon_ali"] forState:UIControlStateNormal];
        [_aliIconButton setImage:[UIImage imageNamed:@"pay_icon_ali"] forState:UIControlStateSelected];
        [_aliIconButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
        [_aliIconButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_aliIconButton setTitle:@"支付宝" forState:UIControlStateNormal];
        [_aliIconButton setTitleColor:BLACKCOLOR forState:UIControlStateSelected];
        [_aliIconButton setTitleColor:GRAYCOLOR forState:UIControlStateNormal];
        _aliIconButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _aliIconButton.adjustsImageWhenHighlighted = NO;
        [_aliIconButton addTarget:self action:@selector(aliSelect) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _aliIconButton;
}

-(UITextField *)aliTextField{
    if (!_aliTextField) {
        _aliTextField = [[UITextField alloc]init];
        _aliTextField.delegate = self;
        _aliTextField.returnKeyType = UIReturnKeyDone;
        _aliTextField.keyboardType = UIKeyboardTypeDefault;
        _aliTextField.userInteractionEnabled = NO;
    }
    return _aliTextField;
}

-(UIView *)moneyView{
    if (!_moneyView) {
        _moneyView = [[UIView alloc]init];
        _moneyView.backgroundColor = [UIColor whiteColor];
    }
    return _moneyView;
}

-(UILabel *)moneyIconLabel{
    if (!_moneyIconLabel) {
        _moneyIconLabel = [[UILabel alloc]init];
        _moneyIconLabel.textColor = BLACKCOLOR;
        _moneyIconLabel.font = [UIFont systemFontOfSize:56];
        _moneyIconLabel.text = @"¥";
    }
    return _moneyIconLabel;
}

-(UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc]init];
        _moneyLabel.textColor = ORANGCOLOR;
        _moneyLabel.font = [UIFont systemFontOfSize:40];
        
    }
    return _moneyLabel;
}


-(UIButton *)withDrawButton{
    if (!_withDrawButton) {
        _withDrawButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _withDrawButton.layer.cornerRadius = 3;
        [_withDrawButton setBackgroundColor:GRAYCOLOR];
        [_withDrawButton setTitle:@"确认提现" forState:UIControlStateNormal];
        [_withDrawButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_withDrawButton addTarget:self action:@selector(comfirmWithDraw) forControlEvents:UIControlEventTouchUpInside];
        _withDrawButton.userInteractionEnabled = NO;
        
    }
    return _withDrawButton;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

@end
